﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic;
using System.Collections;
using PanGu;

namespace WebApp.Product
{
    public partial class Search : System.Web.UI.Page
    {
        private ProductManager m_ProductManager = new ProductManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                string s = Request.QueryString["s"].ToString().Substring(1);

                BindData(s);
            }
        }

        private void BindData(string keyword)
        {
            string panguxml = Server.MapPath("~/pangu/PanGu.xml");
            PanGu.Segment.Init(panguxml);

            PanGu.Segment segment = new PanGu.Segment();
            ICollection<WordInfo> words = segment.DoSegment(keyword);

            string keys = string.Empty;

            foreach (WordInfo item in words)
            {
                keys += item.Word + " ";
            }

            List<Cosmetic.DataEntity.Product> products = this.m_ProductManager.SearchProductByName(keys.TrimEnd());
            this.lvProductSearch.DataSource = products;
            this.lvProductSearch.DataBind();
            this.ltlProductCount.Text = products.Count.ToString();
        }

        protected void lvProductSearch_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "AddCart")
            {
                //ImageButton imgBtn = sender as ImageButton;
                Guid theGuid = new Guid(e.CommandArgument.ToString());
                Cosmetic.DataEntity.Product pif = m_ProductManager.GetProductById(theGuid);
                //if (pif.在库数量 <= 0)
                //{
                //    Page.RegisterStartupScript("openalert", "<script>alert('该商品库存不足，请至商品详细页面预订');</script>");
                //    return;
                //}
                if (pif.eprice == decimal.Parse("0.01"))
                {
                    Page.RegisterStartupScript("openalert", "<script>alert('请播打公司的服务热线：400-6676-996进行询价！');</script>");
                    return;
                }

                Buy(theGuid);
                Response.Redirect("~/Shopping/shopCart.aspx");

                //if (Session["buycarts"] != null)
                //{

                //    Page.RegisterStartupScript("openconfirm ", " <script> if(confirm( '尊敬的用户，由于您选择的产品属于不同\\n的支付系统，所以您的购物车内原有的商\\n品会自动取消,是否继续操作？ ')){self.location='../Shopping/shopCart.htm'} else{self.location='../Shopping/shopCarts.htm'} </script> ");

                //    return;
                //}
                //else
                //{
                //    Response.Redirect("~/Shopping/shopCart.htm");
                //}
            }
        }

        private void Buy(Guid temGuid)
        {
            try
            {
                int theCount = 1;//这里默认为1;

                //add to buy cart
                if (null == Session["buycart"])
                {
                    Session["buycart"] = new Hashtable();
                }
                //加入尺寸
                if (null == Session["size"])
                {
                    Session["size"] = new Hashtable();
                }
                try
                {
                    if (((Hashtable)Session["buycart"]).Contains(temGuid.ToString()))
                    {
                        int tint = int.Parse(((Hashtable)Session["buycart"])[temGuid.ToString()].ToString());
                        ((Hashtable)Session["buycart"])[temGuid.ToString()] = (++tint).ToString();
                    }
                    else
                    {
                        ((Hashtable)Session["buycart"]).Add(temGuid.ToString(), theCount.ToString());
                    }
                    //Response.Redirect("~/Shopping/shopCart.htm");
                }
                catch (Exception)
                {
                    WebGeneric.WebMessageBox("购物篮添加失败!", this.Page);
                }
            }
            catch
            {
                WebGeneric.WebMessageBox("请正确填写购买数量!", this.Page);
            }
        }


        public string spliteStrs(string title)
        {
            if (title.Length > 15)
            {
                title = title.Substring(0, 15) + "…";
            }
            return title;
        }
        public string spliteStr(string title)
        {
            if (title.Length > 10)
            {
                title = title.Substring(0, 10) + "…";
            }
            return title;
        }

        protected void lvProductSearch_PagePropertiesChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                string s = Request.QueryString["s"].ToString();

                BindData(s);
            }
        }

        protected void lvProductSearch_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType== ListViewItemType.DataItem)
            {
                Label lbprice = e.Item.FindControl("lbePrice") as Label;
                Image img = e.Item.FindControl("Image9") as Image;

                //hfePrice.Value != "" && decimal.Parse(hfePrice.Value) == decimal.Parse("0.01")
                if (lbprice.Text != "" && decimal.Parse(lbprice.Text) == decimal.Parse("0.01"))
                {
                    img.Visible = true;
                    lbprice.Visible = false;
                }
                else
                {
                    img.Visible = false;
                    lbprice.Visible = true;
                }
            }
        }
    }
}